06. Getting User Data in Flask — Part 1

Getting user data in Flask Heading

Getting User Data in Flask — Part 1

Methods of getting user data from the view (in Flask)

ND004 C01 L05 06 Getting User Data In Flask

Takeaways

  • There are 3 methods of getting user data from a view to a controller. See the image below.
    • URL query parameters
    • Forms
    • JSON

URL query parameters

  • URL query parameters are listed as key-value pairs at the end of a URL, preceding a "?" question mark. E.g. www.example.com/hello?my_key=my_value .

Form data

  • request.form.get('<name>') reads the value from a form input control (text input, number input, password input, etc) by the name attribute on the input HTML element.

Note: defaults

  • request.args.get , request.form.get both accept an optional second parameter, e.g. request.args.get('foo', 'my default') , set to a default value, in case the result is empty.

JSON

  • request.data retrieves JSON as a string . Then we'd take that string and turn it into python constructs by calling json.loads on the request.data string to turn it into lists and dictionaries in Python.

Three methods of getting user data in Flask

Three methods of getting user data in Flask

Input data quiz

QUIZ QUESTION: :

Match the flask method to the type of user data being handled

ANSWER CHOICES:



Flask Method

User Input

HTTP

JSON

Form input

URL query parameters

SOLUTION:

Flask Method

User Input

JSON

Form input

URL query parameters

Getting user data in Flask Recap

Using HTML form submission to get the data

ND004 C01 L05 06.1 Getting User Data In Flask

Takeaways

  • forms take an action (name of the route) and method (route method) to submit data to our server.
  • The name attribute on a form control element is the key used to retrieve data from request.get(<key>) .
  • All forms either define a submit button, or allow the user to hit ENTER on an input to submit the form.

What should the value of <key> by in request.form.get('<key>') in order to get the value "foo2" from the following HTML form?

<form action="/foo1" method="post">
   <label for="bar0">Bar</label>
  <input type="text" value="foo2" id="bar1" name="bar2" class="bar3">
  <input type="submit" value="Submit" />
</form>
SOLUTION: bar2

Form methods POST vs GET

ND004 C01 L05 06.2 Getting User Data In Flask

Video Correction Notes

  • "Response Body" should be corrected to "Request Body", throughout, since the client is sending off a request .

Takeaways

  • The way form data traverses from the client to server differs based on whether we are using a GET or a POST method on the form.
The POST submission
  • On submit, we send off an HTTP POST request to the route /create with a request body
  • The request body stringifies the key-value pairs of fields from the form (as part of the name attribute) along with their values.
The GET submission
  • Sends off a GET request with URL query parameters that appends the form data to the URL.
  • Ideal for smaller form submissions.

POSTs are ideal for longer form submissions, since URL query parameters can only be so long compared to request bodies (max 2048 characters).
Moreover, forms can only send POST and GET requests, and nothing else.

Methods Quiz

QUIZ QUESTION: :

Match the HTTP request route to the type of form submission it was

ANSWER CHOICES:



Request

Form Submission

A POST form submission

A GET form submission

A PUT form submission

SOLUTION:

Request

Form Submission

A POST form submission

A GET form submission